#ifndef _HTTPUSERDB_H
#define _HTTPUSERDB_H

#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/ptr_container/ptr_map.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

class HTTPUser
{
public:
	
	HTTPUser() : m_username(""), m_password("") {}
	HTTPUser(const std::string & username, const std::string & password)
		: m_username(username), m_password(password) {}
	HTTPUser(const HTTPUser & user)
		: m_username(user.m_username), m_password(user.m_password) {}

	void setUsername(const std::string & username) {m_username = username;}
	std::string getUsername() const {return m_username;}
	void setPassword(const std::string & password) {m_password = password;}
	std::string getPassword() const {return m_password;}
	
private:
	
	std::string m_username;
	std::string m_password;
	
	friend class boost::serialization::access;
	template<class Archive>
	void save(Archive & ar, const unsigned int version) const
	{
		ar & m_username;
		ar & m_password;
	}
	template<class Archive>
	void load(Archive & ar, const unsigned int version)
	{
		ar & m_username;
		ar & m_password;
	}
	BOOST_SERIALIZATION_SPLIT_MEMBER()
};

class HTTPUserDB
{
public:
	
	HTTPUserDB(const std::string & filename);

	bool loadDB();
	bool saveDB();
	int addUser(const HTTPUser & user);
	int delUser(const std::string & name);
	boost::shared_ptr<HTTPUser> getUser(const std::string & name);
	std::vector<std::string> getAllUsername();
	int authUser(const std::string & username, const std::string & password);

	enum DB_ERROR
	{
		SUCCESS = 0,
		ERROR_ENCRYPT,
		ERROR_USEREXISTED,
		ERROR_USERNOTFOUND,
		ERROR_AUTHFAIL
	};
	
private:
	
	boost::ptr_map<std::string, HTTPUser> m_users;
	typedef boost::ptr_map<std::string, HTTPUser>::iterator USER_ITER;
	std::string m_filename;
	
	friend class boost::serialization::access;
	template<class Archive>
	void save(Archive & ar, const unsigned int version) const;
	template<class Archive>
	void load(Archive & ar, const unsigned int version);
	BOOST_SERIALIZATION_SPLIT_MEMBER()
};

#endif
